package demo4_8;

public class Student implements Comparable<Student> {
    //Comparable接口有局限性，只能进行默认的比较
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //根据年龄比较大小
    @Override
    public int compareTo(Student o) {
        //if(this.age>o.age) {
        //return 1;
        //}else if(this.age == o.age) {
        //return 0;
        //}else {
        //return -1;
        //}
        return this.age - o.age;
    }

    //根据姓名比较大小
   /* @Override
    public int compareTo(Student o) {
        if (this.name.compareTo(o.name) > 0) {
            return 1;
        } else if (this.name.compareTo(o.name) == 0) {
            return 0;
        } else {
            return -1;
        }
    }*/
}
